<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>门禁后台-登陆</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="layui/css/layui.css" media="all">
    <link rel="stylesheet" href="layui/layer/layer.css" media="all">
    <link rel="stylesheet" href="layui/laydate/laydate.css" media="all">
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>

    </style>
</head>

<body>

    <table id="gridTable" lay-filter="gridTable"></table>

    <!-- 表格操作列 -->
    <script type="text/html" id="toolbar">
        <a id="btnload" class="layui-btn layui-btn-sm icon-btn" lay-event="load"><i class="layui-icon">&#xe666;</i>刷新</a>
        <!-- <a id="btnAdd" class="layui-btn layui-btn-sm icon-btn" lay-event="add"><i class="layui-icon">&#xe654;</i>新增</a>
        <a id="btnEdit" class="layui-btn layui-btn-warm layui-btn-sm icon-btn" lay-event="edit"><i class="layui-icon">&#xe642;</i>修改</a>
        <a id="btnDelete" class="layui-btn layui-btn-danger layui-btn-sm icon-btn" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</a> -->
    </script>
    <script src="jquery/jquery-3.2.1.min.js"></script>
    <script src="layui/layui.js"></script>
    <script>
        var table;
        layui.use('table', function() {
            table = layui.table;

            //第一个实例
            table.render({
                elem: '#gridTable',
                toolbar: "#toolbar",
                height: 800,
                data: [],
                page: true, //开启分页
                cols: [
                    [{
                        type: 'checkbox',
                        fixed: 'left',
                        overflow: 'hiden'
                    }, {
                        type: 'numbers',
                        title: '',
                        fixed: 'left',
                        title: '',
                    }, {
                        field: 'CardNo',
                        title: '卡号',
                        width: 120
                    }, {
                        field: 'CardDate',
                        title: '刷卡时间',
                        width: 180,
                        sort: true
                    }]
                ]
            });

            loadTable();


            /* 表格头工具栏点击事件 */
            table.on('toolbar(gridTable)', function(obj) {
                if (obj.event === 'add') { // 添加
                    showEditModel();
                } else if (obj.event === 'edit') { // 修改
                    var checkRows = table.checkStatus('gridTable');
                    if (checkRows.data.length === 0 || checkRows.data.length > 1) {
                        layer.msg('请选择且只选择一条数据', {
                            icon: 2
                        });
                        return;
                    }
                    showEditModel(checkRows.data[0], "update");
                } else if (obj.event === 'del') { // 删除
                    var checkRows = table.checkStatus('gridTable');
                    if (checkRows.data.length === 0) {
                        layer.msg('请选择要删除的数据', {
                            icon: 2
                        });
                        return;
                    }
                    doDel(checkRows.data, function() {
                        loadTable();
                    });
                } else if (obj.event === 'load') { // 刷新
                    loadTable();
                }
            });

            /* 显示表单弹窗 */
            function showEditModel(mData, type) {
                var CardNo = "";
                var ValidityDate = "";
                if (mData != undefined && mData != null) {
                    CardNo = mData.CardNo;
                    ValidityDate = mData.ValidityDate;
                }
                layer.open({
                    type: 2,
                    area: ['25%', '60%'],
                    title: mData ? '修改' : '添加',
                    content: 'jurisdictionForm.html?type=' + type + '&CardNo=' + CardNo + '&ValidityDate=' + ValidityDate,
                });
            }

            /* 删除 */
            function doDel(obj, action) {
                layer.confirm('确定要删除选中数据吗？', {
                    skin: 'layui-layer-admin',
                    shade: .1
                }, function(i) {
                    layer.close(i);
                    var loadIndex = layer.load(2);
                    $.ajax({
                        url: "/JurisdictionDelete",
                        data: {
                            CardNos: JSON.stringify(obj)
                        },
                        method: "post",
                        async: false,
                        success: function(res) {
                            layer.close(loadIndex);
                            if (1 === res.Tag) {
                                layer.msg("操作成功", {
                                    icon: 1
                                });
                                action();
                            } else {
                                layer.msg(res.Message, {
                                    icon: 2
                                });
                            }
                        },
                        error: function() {
                            layer.close(loadIndex);
                        }
                    });
                });
            }
        });
        //加载数据
        function loadTable() {
            var upload = layer.msg('加载中', {
                icon: 16,
                shade: 0.2,
                time: false
            });
            $.get(`/jurisdiction/mjdata.json`, function(res, status) {
                res = JSON.parse(res);
                if (res != undefined && res.length > 0) {
                    table.reload('gridTable', {
                        url: '',
                        data: res,
                    });
                }
                layer.close(upload);
            });
        }
    </script>
</body>

</html>